package org.theonept.travian.t4a.message;

import java.util.ArrayList;
import java.util.List;

import org.theonept.travian.model.Message;
import org.theonept.travian.t4a.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListMessageAdapter extends ArrayAdapter<Message> {
	private List<Message> data = new ArrayList<Message>();

	/**
	 * @return the data
	 */
	public List<Message> getData() {
		return data;
	}

	/**
	 * @param data
	 *            the data to set
	 */
	public void setData(List<Message> data) {
		this.data = data;
	}

	private final Activity context;

	public ListMessageAdapter(Activity context) {

		super(context, android.R.id.text1);
		this.context = context;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Message getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return Long.valueOf(data.get(position).getId());
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Message message = data.get(position);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.message_list_view, parent,
				false);
		TextView messageSender = (TextView) rowView
				.findViewById(R.id.message_sender);
		TextView messageSubject = (TextView) rowView
				.findViewById(R.id.message_subject);
		messageSender.setText(message.getExpediteur());
		messageSubject.setText(message.getSujet());
		if (!message.getRead()) {
			rowView.setBackgroundColor(rowView.getResources().getColor(
					R.color.colorUnread));
		}
		return rowView;
	}

}
